package mobi.hami.huapin.service.impl;

import com.github.pagehelper.PageHelper;
import mobi.hami.huapin.config.AppConfig;
import mobi.hami.huapin.core.stock.StockUtil;
import mobi.hami.huapin.dao.SpuSubjectMappingMapper;
import mobi.hami.huapin.dao.SubjectMapper;
import mobi.hami.huapin.dto.SKUPresentDTO;
import mobi.hami.huapin.dto.SkuStockDTO;
import mobi.hami.huapin.model.*;
import mobi.hami.huapin.service.SubjectService;
import mobi.hami.huapin.util.BeanUtil;
import mobi.hami.huapin.util.StringUtil;
import mobi.hami.huapin.util.Weeks;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 类说明
 *
 * @author 阿飞 jijunjian@hami.mobi
 * @date 2018/8/28 9:39
 */
@Service
public class SubjectServiceImpl implements SubjectService {

    private static final Logger logger = LoggerFactory.getLogger(SubjectServiceImpl.class);

    @Autowired
    private SubjectMapper subjectMapper;

    @Autowired
    private SpuSubjectMappingMapper subjectMappingMapper;

    @Autowired
    AppConfig appConfig;
    @Autowired
    private StockUtil stockUtil;

    /**
     * 返回所有
     *
     * @return
     */
    @Override
    @Cacheable(value = "getAllSubject")
    public List<Subject> getAllSubject() {

        logger.info("没有缓存  getAllSubject");

        SubjectExample example = new SubjectExample();
        SubjectExample.Criteria criteria = example.createCriteria();
        criteria.andUse_statusEqualTo(1);
        criteria.andDeletedEqualTo(0);

        PageHelper.startPage(1, 100);
        List<Subject> list = subjectMapper.selectByExample(example);



        return list;
    }

    /**
     * 根据日期返回当天的主题
     *
     * @param date
     * @return
     */
    @Override
    public Subject getTodaySubject(Date date) {

        int day_of_week = Weeks.getDayOfWeek(date);

        Subject today_subject = new Subject();




        List<Subject> subjectList = getAllSubject();
     //   logger.info("day_of_week;"+day_of_week);
       // logger.info("subjectList;"+subjectList.size());

        subjectList.forEach(a ->{

            //logger.info("subjectList.forEach;"+a.getDay_of_week());

            if (a.getDay_of_week() == day_of_week)
            {
                BeanUtil.copyProperties(a,today_subject);
                return ;
            }
        });

        return today_subject;
    }

    /**
     * 返回某个主题的spu列表
     *
     * @param subject_id
     * @return
     */
    //@Cacheable(value = "getSpuBySubjectID",keyGenerator="firstParamKeyGenerator")
    //@Cacheable(value = "getSpuBySubjectID",key="'getSpuBySubjectID'+#subject_id")
    @Override
    public List<SpuSubjectMapping> getSpuBySubjectID(long subject_id) {

        logger.info("没有缓存  getSpuBySubjectID");

        short deleted = 0;

        SpuSubjectMappingExample example = new SpuSubjectMappingExample();
        SpuSubjectMappingExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(deleted);
        criteria.andSubject_idEqualTo(subject_id);

        PageHelper.startPage(1, appConfig.defaultfoodcount);
        List<SpuSubjectMapping> list = subjectMappingMapper.selectByExample(example);

        return list;
    }

    /**
     * 根据主题获取赠送的SKU信息
     * @param id
     * @return
     */
    @Override
    public List<SKUPresentDTO> getSKUPreSentListBySubjectId(Integer id) {

        List<SKUPresentDTO> presentDTOS = subjectMapper.getSKUPreSentListBySubjectId(id);;
        final StringBuilder skuids = new StringBuilder();
        presentDTOS.forEach(a->
        {
            skuids.append(a.getId()+",");
        });
        if (skuids.toString().length() > 0)
        {
            String sku_ids = StringUtil.deleteLastLetter(skuids.toString());
            List<SkuStockDTO> stockDTOS = stockUtil.queryBat(sku_ids,"");

            presentDTOS.forEach(product->{

                product.setStock(0);
                stockDTOS.forEach(a ->{
                    if (product.getId() == (long)(a.getSku_id()))
                    {
                        product.setStock(a.getNum()*99);
                        return;
                    }
                });
            });

        }

       return presentDTOS;

    }
}
